﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QualityEvaluatingTools.model;
using QualityEvaluatingTools.helper;

namespace QualityEvaluatingTools.manager
{
    /// <summary>
    /// 学生成绩管理
    /// </summary>
    public class StuAnswerManager
    {
        #region 对外方法

        /// <summary>
        /// 获取学生综合评测统计信息
        /// </summary>
        /// <returns></returns>
        public static List<Evaluation> GetStuEvaluationMsg(string excelPath, System.Windows.Forms.ProgressBar progressBar)
        {
            //加载xml树
            string currentPath = System.IO.Directory.GetCurrentDirectory();
            System.Xml.Linq.XDocument baseDoc = XmlHelper.GetBasicRuleDoc(currentPath + "/document/BasicRule.xml");

            //获取学生答卷
            List<Evaluation> evaluationList = GetStuEvaluation(excelPath);

            if (evaluationList == null)
                return null;

            progressBar.Maximum = evaluationList.Count;

            //循环计算每个学生的分数
            foreach (Evaluation item in evaluationList)
            {
                Dictionary<String, EvaluationItem> map = new Dictionary<string, EvaluationItem>();

                //1：计算意志力测评份数
                map.Add(EnumUtils.GetName(EvaluationType.YiZhiLI), CalculateManager.GetYiZhiLiScore(item, XmlHelper.GetEvaluationXmlDoc(EvaluationType.YiZhiLI, baseDoc)));
                //3:自制力测评
                map.Add(EnumUtils.GetName(EvaluationType.ZiZhiLi), CalculateManager.GetZiZhiLiScore(item, XmlHelper.GetEvaluationXmlDoc(EvaluationType.ZiZhiLi, baseDoc)));
                //5:心里适应能力
                map.Add(EnumUtils.GetName(EvaluationType.XinLiShiYinNengLi), CalculateManager.GetXinLiShiYingNengLiScore(item, XmlHelper.GetEvaluationXmlDoc(EvaluationType.XinLiShiYinNengLi, baseDoc)));
                //7:乐观指数
                map.Add(EnumUtils.GetName(EvaluationType.LeGuangZhiShu), CalculateManager.GetLeGuanZhiShuScore(item, XmlHelper.GetEvaluationXmlDoc(EvaluationType.LeGuangZhiShu, baseDoc)));
                //9:智力测评
                map.Add(EnumUtils.GetName(EvaluationType.ZhiLi), CalculateManager.GetZhiLICePingScore(item, XmlHelper.GetEvaluationXmlDoc(EvaluationType.ZhiLi, baseDoc)));

                //2：压力测试
                map.Add(EnumUtils.GetName(EvaluationType.YaLi), CalManager.GetPressureTestScore(item, XmlHelper.GetEvaluationXmlDoc(EvaluationType.YaLi, baseDoc)));
                //4：心理承受能力测试
                map.Add(EnumUtils.GetName(EvaluationType.XinLiChenShouNengLi), CalManager.GetPsychologicalBearTestScore(item, XmlHelper.GetEvaluationXmlDoc(EvaluationType.XinLiChenShouNengLi, baseDoc)));
                //6：自主性测试
                map.Add(EnumUtils.GetName(EvaluationType.ZiZhuXing), CalManager.GetAutonomyTestScore(item, XmlHelper.GetEvaluationXmlDoc(EvaluationType.ZiZhuXing, baseDoc)));
                //8：交际能力测试
                map.Add(EnumUtils.GetName(EvaluationType.JiaoJiNengLi), CalManager.GetCommunicationAbilityScore(item, XmlHelper.GetEvaluationXmlDoc(EvaluationType.JiaoJiNengLi, baseDoc)));
                //10：职业性格测试
                map.Add(EnumUtils.GetName(EvaluationType.ZhiYeXingGe), CalManager.GetOccupationalCharacterTestResult(item, XmlHelper.GetEvaluationXmlDoc(EvaluationType.ZhiYeXingGe, baseDoc)));

                progressBar.Value++;
                item.EvaluationItemMap = map;
            }
            return evaluationList;
        }

        /// <summary>
        /// 获取原型展示
        /// </summary>
        /// <param name="evaluationList"></param>
        /// <returns></returns>
        public static List<DataModel> GetDataModelList(List<Evaluation> evaluationList)
        {
            List<DataModel> list = new List<DataModel>();

            int i = 0;
            foreach (var item in evaluationList)
            {
                DataModel model = new DataModel();
                model.Num = ++i;
                model.AccountName = item.AccountName;
                model.StuName = item.StudentName;
                model.LeGuan = GetScoreStr(EvaluationType.LeGuangZhiShu, item);
                model.JiaoJi = GetScoreStr(EvaluationType.JiaoJiNengLi, item);
                model.XinLiChenShou = GetScoreStr(EvaluationType.XinLiChenShouNengLi, item);
                model.XinLiShiYin = GetScoreStr(EvaluationType.XinLiShiYinNengLi, item);
                model.YaLi = GetScoreStr(EvaluationType.YaLi, item);
                model.YiZhiLi = GetScoreStr(EvaluationType.YiZhiLI, item);
                model.ZhiLi = GetScoreStr(EvaluationType.ZhiLi, item);
                model.ZhiYe = GetOccupationalCharacterTestResult(EvaluationType.ZhiYeXingGe, item);
                model.ZiZhiLi = GetScoreStr(EvaluationType.ZiZhiLi, item);
                model.ZiZhu = GetScoreStr(EvaluationType.ZiZhuXing, item);
                list.Add(model);
            }
            return list;
        }


        #endregion

        #region 自定义辅助函数

        /// <summary>
        /// 获得每项的分数
        /// </summary>
        /// <param name="type"></param>
        /// <param name="evaluation"></param>
        /// <returns></returns>
        private static string GetScoreStr(EvaluationType type, Evaluation evaluation)
        {
            string typeStr = EnumUtils.GetName(type);
            return evaluation.EvaluationItemMap.ContainsKey(typeStr) ? evaluation.EvaluationItemMap[typeStr].Score.ToString() : "0";
        }

        /// <summary>
        /// 获得职业性格的测评结果
        /// </summary>
        /// <param name="type"></param>
        /// <param name="evaluation"></param>
        /// <returns></returns>
        private static string GetOccupationalCharacterTestResult(EvaluationType type, Evaluation evaluation)
        {
            string typeStr = EnumUtils.GetName(type);
            return evaluation.EvaluationItemMap.ContainsKey(typeStr) ? evaluation.EvaluationItemMap[typeStr].result.ToString() : "0";
        }

        /// <summary>
        /// 获取所有考试学生的信息（包含学生答案）
        /// </summary>
        /// <param name="excelPath"></param>
        /// <returns></returns>
        private static List<Evaluation> GetStuEvaluation(string excelPath)
        {
            return ExcelHelper.GetStuEvaluationWithAnwer(excelPath);
        }

        #endregion

    }
}
